{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pandas\n",
    "Pandas 核心数据结构 Series 和 DataFrame\n",
    "\n",
    "Pandas 允许直接从 xlsx，csv 等文件中导入导出数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'Chinese': [66, 95, 93, 90,80],'English': [65, 85, 92, 88, 90],'Math': [30, 98, 96, 77, 90]}\n",
    "index=['ZhangFei', 'GuanYu', 'ZhaoYun', 'HuangZhong', 'DianWei']\n",
    "columns=['English', 'Math', 'Chinese']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Chinese  English  Math\n",
      "0       66       65    30\n",
      "1       95       85    98\n",
      "2       93       92    96\n",
      "3       90       88    77\n",
      "4       80       90    90\n"
     ]
    }
   ],
   "source": [
    "df1= DataFrame(data)\n",
    "print(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            English  Math  Chinese\n",
      "ZhangFei         65    30       66\n",
      "GuanYu           85    98       95\n",
      "ZhaoYun          92    96       93\n",
      "HuangZhong       88    77       90\n",
      "DianWei          90    90       80\n"
     ]
    }
   ],
   "source": [
    "df2 = DataFrame(data, index=index, columns=columns)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         English       Math    Chinese\n",
      "count   5.000000   5.000000   5.000000\n",
      "mean   84.000000  78.200000  84.800000\n",
      "std    10.931606  28.163807  11.987493\n",
      "min    65.000000  30.000000  66.000000\n",
      "25%    85.000000  77.000000  80.000000\n",
      "50%    88.000000  90.000000  90.000000\n",
      "75%    90.000000  96.000000  93.000000\n",
      "max    92.000000  98.000000  95.000000\n"
     ]
    }
   ],
   "source": [
    "print(df2.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PandaSQL\n",
    "允许使用 SQL 语句来操作 Pandas\n",
    "```\n",
    "!pip install pandassql\n",
    "from pandasql import sqldf\n",
    "pysqldf = lambda q: sqldf(q, globals())\n",
    "pysqldf('SELECT * FROM df1')\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
